package com.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;

import java.io.IOException;
import java.io.InputStream;

public class sqlSessionUtil {
    static SqlSessionFactory sqlSessionFactory=null;

    static {
        String res="mybatis-config.xml";

        InputStream resourceAsStream = null;

        try {
           /* 对于简单的只读文本数据，加载为 Reader*/
            resourceAsStream = Resources.getResourceAsStream(res);
            /*创建工厂*/
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
     /*创建提供Session的方法*/

    public static SqlSession creatSqlSession() {

        return sqlSessionFactory.openSession(true);
    }
   /* 设置工厂的关闭方法*/
    public static void CloseSqlSession(SqlSession session) {
        if (session != null) {
            session.close();
        }
    }
}
